/* 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package com.googlecode.hmvc4gwt.example.hmvcblog.frontend.dashboard.client.dashboard;

import java.util.HashMap;
import java.util.List;

import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.client.applicationframe.events.in.AddApplicationViewEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.dashboard.client.dashboard.events.in.ShowDashboardViewEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.historytoken.client.HistoryToken;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.i18n.client.I18N;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.blog.BlogRemoveDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.dashboard.UserBlogTitleDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.events.in.GetListUserBlogTitleEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.events.in.RemoveBlogEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.events.out.BlogNotFoundEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.events.out.BlogSuccessfulRemovedEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.events.out.NotBlogOwnerEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.blog.events.out.ReceiveListBlogTitleDTOEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.events.NotLoggedInEvent;
import com.googlecode.hmvc4java.event.IEvent;
import com.googlecode.hmvc4java.registry.controller.AControllerModelViewRegistry;

/**
 * 
 * @author jens.meiss
 */
public class DashboardController extends AControllerModelViewRegistry<DashboardModel, DashboardView> {

	public void handleEvent(IEvent event) {
		
		if (event.getClass() == ShowDashboardViewEvent.class) {
			
			fireEventGlobal(new AddApplicationViewEvent(getView().getLabelTitle(), getView().getWidget()));
			fireEventGlobal(new GetListUserBlogTitleEvent(this));
			
		} else if (event.getClass() == ReceiveListBlogTitleDTOEvent.class) {
			
			refreshBlogList(((ReceiveListBlogTitleDTOEvent) event).getListBlogPreviewDTO());
			
		} else if (event.getClass() == NotLoggedInEvent.class) {
			
			Window.alert(I18N.constants.messageNotLoggedIn());
			History.newItem(HistoryToken.bloglist);
			
		} else if (event.getClass() == BlogSuccessfulRemovedEvent.class) {
			
			fireEventGlobal(new GetListUserBlogTitleEvent(this));
			
		} else if (event.getClass() == BlogNotFoundEvent.class) {
			
			Window.alert(I18N.constants.messageBlogNotFound());
			
		} else if (event.getClass() == NotBlogOwnerEvent.class) {
			
			Window.alert(I18N.constants.messageNotBlogOwner());
			
		}
		
	}

	public void init() {
		
		register(ShowDashboardViewEvent.class);
		register(ReceiveListBlogTitleDTOEvent.class);
		register(NotLoggedInEvent.class);
		register(BlogSuccessfulRemovedEvent.class);
		register(BlogNotFoundEvent.class);
		register(NotBlogOwnerEvent.class);
		
		initView();
				
	}
	
	public void initView() {

		AbsolutePanel absolutePanelContainer = new AbsolutePanel();
		getView().setAbsolutePanelContainer(absolutePanelContainer);
		
		Label labelTitle = new Label(I18N.constants.labelDashboard());
		getView().setLabelTitle(labelTitle);

		Hyperlink hyperlinkEditUserData = new Hyperlink(I18N.constants.linkEditUserData(), HistoryToken.editUserdata);
		getView().setEditUserData(hyperlinkEditUserData);
		
		Hyperlink hyperlinkLogout = new Hyperlink(I18N.constants.linkLogout(), HistoryToken.logout);
		getView().setLogout(hyperlinkLogout);
		
		FlexTable flexTableBloglist = new FlexTable();
		flexTableBloglist.setStylePrimaryName("tableBlogList");
		getView().setFlexTableBlogList(flexTableBloglist);
		
		Label labelBloglist = new Label(I18N.constants.labelUserBlogList());
		getView().setLabelBloglist(labelBloglist);

		Hyperlink hyperlinkAddBlog = new Hyperlink(I18N.constants.labelAddBlog() , HistoryToken.addBlog);
		getView().setHyperlinkAddBlog(hyperlinkAddBlog);
		
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel.setStylePrimaryName("panelMenu");
		horizontalPanel.setSpacing(3);
		horizontalPanel.add(getView().getEditUserData());
		horizontalPanel.add(getView().getLogout());
		
		absolutePanelContainer.add(horizontalPanel);
		absolutePanelContainer.add(flexTableBloglist);
		absolutePanelContainer.add(hyperlinkAddBlog);

	};


	private void refreshBlogList(List<UserBlogTitleDTO> listUserBlogTitle) {
		
		FlexTable flexTable = getView().getFlexTableBlogList();
		flexTable.clear();
		
		getModel().getMapHyperlinkRemove().clear();
		
		flexTable.setWidget(0, 0, getView().getLabelBloglist());
		
		if ((listUserBlogTitle == null) || (listUserBlogTitle.size() == 0)) {
			return;
		}
		
		ClickListener clickListener = new ClickListener() {
			public void onClick(Widget sender) {
				removeBlog(getModel().getMapHyperlinkRemove().get(sender));
			}
		};
		
		int cnt = 1;
		for (UserBlogTitleDTO userBlogTitleDTO : listUserBlogTitle) {
			
			Hyperlink hyperlinkShow 	= new Hyperlink(userBlogTitleDTO.getTitle() , HistoryToken.blog + "=" + userBlogTitleDTO.getId());
			Hyperlink hyperlinkEdit		= new Hyperlink(I18N.constants.linkEditBlog() , HistoryToken.editBlog + "=" + userBlogTitleDTO.getId());
			
			Hyperlink hyperlinkRemove 	= new Hyperlink(I18N.constants.linkRemoveBlog(), "");
			hyperlinkRemove.addClickListener(clickListener);
			
			getModel().getMapHyperlinkRemove().put(hyperlinkRemove, userBlogTitleDTO);
			
			flexTable.setWidget(cnt, 0, hyperlinkShow);
			flexTable.setWidget(cnt, 1, hyperlinkEdit);
			flexTable.setWidget(cnt, 2, hyperlinkRemove);			
			
			cnt++;
		}
		
//		flexTable.setWidget(cnt, 2, getView().getHyperlinkAddBlog());
		
	}
	
	private void removeBlog(UserBlogTitleDTO userBlogTitleDTO) {
		
		if (Window.confirm(I18N.constants.messageConfirmBlogRemove() + " " + userBlogTitleDTO.getTitle())) {
			
			BlogRemoveDTO blogRemoveDTO = new BlogRemoveDTO();
			blogRemoveDTO.setId(userBlogTitleDTO.getId());
			
			fireEventGlobal(new RemoveBlogEvent(blogRemoveDTO, this));
			
		};
		
		fireEventGlobal(new GetListUserBlogTitleEvent(getComponent().getController()));
	}

	public void terminate() {
	}

	public void initModel() {
		
		getModel().setMapHyperlinkRemove(new HashMap<Hyperlink, UserBlogTitleDTO>());
		
	}

}
